load("@jupyter-pip-requirements//:requirements.bzl", "all_data_requirements", "all_requirements", "all_whl_requirements")
load("@jupyter-pip-requirements-dev//:requirements.bzl", all_data_requirements_dev = "all_data_requirements", all_requirements_dev = "all_requirements", all_whl_requirements_dev = "all_whl_requirements")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
load("@rules_pyvenv//:venv.bzl", "py_venv")

# gazelle:python_extension enabled
# gazelle:python_test_naming_convention test_$package_name$
# gazelle:resolve py jupyterlab_pachyderm //jupyter-extension/jupyterlab_pachyderm

exports_files([
    "requirements.txt",
    "requirements-dev.txt",
])

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
modules_mapping(
    name = "modules_map",
    wheels = all_whl_requirements + all_whl_requirements_dev,
)

# To create lock files: bazel run :requirements.update
compile_pip_requirements(
    name = "requirements",
    src = "requirements.txt",
    requirements_txt = "requirements-lock.txt",
)

# To create lock files: bazel run :requirements-dev.update
compile_pip_requirements(
    name = "requirements-dev",
    src = "requirements-dev.txt",
    extra_args = ["--no-annotate"],
    requirements_txt = "requirements-dev-lock.txt",
)

# Gazelle python extension needs a manifest file mapping from
# an import to the installed package that provides it.
# This macro produces two targets:
# - :gazelle_python_manifest.update can be used with `bazel run`
#   to recalculate the manifest
# - :gazelle_python_manifest.test is a test target ensuring that
#   the manifest doesn't need to be updated
gazelle_python_manifest(
    name = "gazelle_python_manifest",
    modules_mapping = ":modules_map",
    # This is what we called our `pip_parse` rule, where third-party
    # python libraries are loaded in BUILD files.
    pip_repository_name = "jupyter-pip-requirements",
    # This should point to wherever we declare our python dependencies
    # (the same as what we passed to the modules_mapping rule in WORKSPACE)
    # This argument is optional. If provided, the `.test` target is very
    # fast because it just has to check an integrity field. If not provided,
    # the integrity field is not added to the manifest which can help avoid
    # merge conflicts in large repos.
    requirements = ":requirements",
)

# Create a virtual environment at ./venv: bazel :venv
py_venv(
    name = "venv",
    data = all_data_requirements + all_data_requirements_dev,
    extra_pip_commands = [
        # Important! ref: https://github.com/cedarai/rules_pyvenv/issues/15
        "install --ignore-installed setuptools",
    ],
    venv_location = "jupyter-extension/venv",
    deps = all_requirements + all_requirements_dev,
)

py_library(
    name = "jupyter-extension",
    srcs = ["scripts/jupyter_server_test_config.py"],
    visibility = ["//:__subpackages__"],
)
